<?php
/*
 +----------------------------------------------------------------------
 + Title        : ApiAuth 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/6/1 10:04
 + Last-time    : 2019/6/1 10:04+ 86187
 + Desc         : ApiAuth
 +----------------------------------------------------------------------
*/


namespace app\admin\controller;


use library\Controller;
use library\tools\Data;
use think\Db;

class ApiAuth extends Controller
{
    use \app\common\traits\controller\Controller;
    protected $table = 'api_auth';

    /**
     * 列表
     * @auth true
     * @menu true
     * @return mixed
     * @author : Randy_chen
     * @Date   : 2019/7/13
     * @Time   : 10:57
     */
    public function index()
    {
        $this->title = '接口权限管理';
        if ($this->request->post()) {
            $res = $this->_query($this->table)
                ->whereNull('delete_time')
                        ->order('id desc')
                         ->page(true, false ,'',request()->param('limit'));
            $res['data'] = $res['list'];
            $this->success('查询成功!', $res);
        } else {
            $this->applyCsrfToken();
            return $this->fetch();
        }
    }

    protected function _index_page_filter(&$data)
    {
        $data = Data::arr2table($data);
    }

    /**
     *  接口授权
     * @return mixed|void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     *
     *
     * @author : Randy_chen
     * @Date   : 2019/6/17
     * @Time   : 11:55
     */
    public function apply()
    {
        $this->title = '权限配置节点';
        $auth = $this->request->post('id', '0');
        switch (strtolower($this->request->post('action'))) {
            case 'get': // 获取权限配置
                $api_auth = new \app\admin\service\ApiAuth();
                $nodes = $api_auth->get();
                $checked = Db::name('api_user_auth')->where(['ac_id' => $auth])->column('auth_id');
                foreach ($nodes as &$node) {
                    $node['checked'] = \in_array($node['id'], $checked, true);
                }
                $nodes = Data::arr2tree($nodes, 'id', 'pid', '_sub_') ;
                $data = $this->_apply_filter($nodes);
                return $this->success('获取权限配置成功！', $data);
            case 'gets': // 获取权限配置
                $api_auth = new \app\admin\service\ApiAuth();
                $nodes = $api_auth->get();
                $checked = Db::name('api_user_auth')->where(['ac_id' => $auth])->column('auth_id');
                foreach ($nodes as &$node) {
                    $node['checked'] = \in_array($node['id'], $checked, true);
                }
                $nodes = Data::arr2tree($nodes, 'id', 'pid', 'children') ;
                $data = $this->_apply_filter($nodes);
                $res['tree']  = $data;
                $res['auth']  = $checked;
                return $this->success('获取权限配置成功！', $res);
            case 'save': // 保存权限配置
                list($post, $data) = [
                    $this->request->post(),
                    []
                ];
                foreach (isset($post['nodes']) ? $post['nodes'] : [] as $node) {
                    $data[] = [
                        'ac_id' => $auth,
                        'auth_id' => $node
                    ];
                }
                Db::name('api_user_auth')
                  ->where(['ac_id' => $auth])
                  ->delete();
                Db::name('api_user_auth')
                  ->insertAll($data);

                return $this->success('权限授权配置更新成功！');
            default:
                return $this->_form($this->table, 'apply');
        }
    }

    private function _apply_filter($nodes, $level = 1)
    {
        foreach ($nodes as $key => $node) {
            if (!empty($node['_sub_']) && \is_array($node['_sub_'])) {
                $node[$key]['_sub_'] = $this->_apply_filter($node['_sub_'], $level + 1);
            }
        }
        return $nodes;
    }

    public function _form_filter(&$data)
    {
        $category = \db('api_auth')->where('pid' , 0) ->select();

        $this->assign('category', $category);
    }
}